package com.ruoyi.system.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Component("StatusSchedule")
@RequiredArgsConstructor
public class StatusSchedule {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * @describe 公告信息表status定时更新
     * @params []
     * @author SUNLIANG
     * @date 2023/11/13
     */
    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai") // 每天零点执行 默认使用服务器默认时区
    @Transactional(rollbackFor = Exception.class)
    public void status() {

        List<SysNotice> sysNotices = sysNoticeMapper.selectList(new LambdaQueryWrapper<SysNotice>().eq(SysNotice::getStatus,1));

        for (SysNotice sysNotice : sysNotices) {
            System.out.println(sysNotice);
            //公告状态（0-已发布 1-待发布 2-已下架）
            if (sysNotice.getNoticeReleaseTime().after(DateUtils.getNowDate()) || sysNotice.getNoticeReleaseTime().equals(DateUtils.getNowDate())) {
                sysNotice.setStatus(0);
                sysNoticeMapper.updateById(sysNotice);
            }
        }
    }


}
